package com.chj.gaoji.class04;

import java.util.Arrays;

//https://www.cnblogs.com/xiaomoxian/p/5186762.html
//https://www.jianshu.com/p/5ad9ca0e3cf6
public class Code06_BFPRT {

	public static int[] getMinKNumsByBFPRT(int[] arr, int k) {
		if (k < 1 || k > arr.length) {
			return arr;
		}
		int minKth = getMinKthByBFPRT(arr, k);
		int[] res = new int[k];
		int index = 0;
		for (int i = 0; i != arr.length; i++) {
			if (arr[i] < minKth) {
				res[index++] = arr[i];
			}
		}
		for (; index != res.length; index++) {
			res[index] = minKth;
		}
		return res;
	}

	public static int getMinKthByBFPRT(int[] arr, int K) {
		int[] copyArr = copyArray(arr);
		return bfprt(copyArr, 0, copyArr.length - 1, K - 1);
	}

	public static int[] copyArray(int[] arr) {
		int[] res = new int[arr.length];
		for (int i = 0; i != res.length; i++) {
			res[i] = arr[i];
		}
		return res;
	}

	// 在arr[begin..end]范围上，求如果排序的话，i位置的数是谁，返回
	// i一定在begin~end范围上
	public static int bfprt(int[] arr, int begin, int end, int i) {
		if (begin == end) {
			return arr[begin];
		}
		// 分组 + 组内排序 + 组成newarr + 选出newarr的上中位数 pivot
		int pivot = medianOfMedians(arr, begin, end);
		// 根据pivot做划分值 <p ==p >p ，返回等于区域的左边界和右边界
		// pivotRange[0] 等于区域的左边界
		// pivotRange[1] 等于区域的右边界
		int[] pivotRange = partition(arr, begin, end, pivot);
		if (i >= pivotRange[0] && i <= pivotRange[1]) {
			return arr[i];
		} else if (i < pivotRange[0]) {
			return bfprt(arr, begin, pivotRange[0] - 1, i);
		} else {
			return bfprt(arr, pivotRange[1] + 1, end, i);
		}
	}

	public static int medianOfMedians(int[] arr, int begin, int end) {
		int num = end - begin + 1;
		int offset = num % 5 == 0 ? 0 : 1;
		int[] mArr = new int[num / 5 + offset];
		for (int i = 0; i < mArr.length; i++) {
			int beginI = begin + i * 5;
			int endI = beginI + 4;
			mArr[i] = getMedian(arr, beginI, Math.min(end, endI));
		}
		return bfprt(mArr, 0, mArr.length - 1, mArr.length / 2);
	}

	// 荷兰国旗问题
	public static int[] partition(int[] arr, int L, int R, int p) {
		int less = L - 1; // <区的右边界
		int more = R + 1; // >区的左边界
		int index = L;
		while (index < more) { // L是当前数的下标
			if (arr[index] < p) {
				less += 1;
				swap(arr, less, index);
				index += 1;
			} else if (arr[index] > p) {
				more -= 1;
				swap(arr, more, index);
			} else {
				index += 1;
			}
		}

		int[] range = new int[2];
		range[0] = less + 1;
		range[1] = more - 1;
		return range;
	}

//	public static int[] partition(int[] arr, int begin, int end, int pivotValue) {
//		int small = begin - 1;
//		int cur = begin;
//		int big = end + 1;
//		while (cur != big) {
//			if (arr[cur] < pivotValue) {
//				swap(arr, ++small, cur++);
//			} else if (arr[cur] > pivotValue) {
//				swap(arr, cur, --big);
//			} else {
//				cur++;
//			}
//		}
//		int[] range = new int[2];
//		range[0] = small + 1;
//		range[1] = big - 1;
//		return range;
//	}

	public static int getMedian(int[] arr, int begin, int end) {
		insertionSort(arr, begin, end);
//		int sum = end + begin;
//		int mid = (sum / 2) + (sum % 2);
		int mid = begin + ((end - begin) >> 1);
		return arr[mid];
	}

	public static void insertionSort(int[] arr, int left, int right) {
		if (arr == null || arr.length < 2) {
			return;
		}
		for (int i = left + 1; i < right; i++) {
			for (int j = i - 1; j >= left && arr[j] > arr[j + 1]; j--) {
				swap(arr, j, j + 1);
			}
		}
	}

//	public static void insertionSort(int[] arr, int begin, int end) {
//		for (int i = begin + 1; i != end + 1; i++) {
//			for (int j = i; j != begin; j--) {
//				if (arr[j - 1] > arr[j]) {
//					swap(arr, j - 1, j);
//				} else {
//					break;
//				}
//			}
//		}
//	}

	public static void swap(int[] arr, int index1, int index2) {
		int tmp = arr[index1];
		arr[index1] = arr[index2];
		arr[index2] = tmp;
	}

	public static void printArray(int[] arr) {
		for (int i = 0; i != arr.length; i++) {
			System.out.print(arr[i] + " ");
		}
		System.out.println();
	}

	// for test
	public static int[] generateRandomArray(int maxSize, int maxValue) {
		int[] arr = new int[(int) ((maxSize + 1) * Math.random())];
		for (int i = 0; i < arr.length; i++) {
			arr[i] = (int) ((maxValue + 1) * Math.random()) - (int) (maxValue * Math.random());
		}
		return arr;
	}

	// for test
	public static int[] comparator(int[] arr, int k) {
		if (k < 1 || k > arr.length) {
			return arr;
		}
		Arrays.sort(arr);
		int[] res = new int[k];
		int index = 0;
		for (int i = 0; i != k; i++) {
			res[index++] = arr[i];
		}
		return res;
	}

	// for test
	public static boolean isEqual(int[] arr1, int[] arr2) {
		if ((arr1 == null && arr2 != null) || (arr1 != null && arr2 == null)) {
			return false;
		}
		if (arr1 == null && arr2 == null) {
			return true;
		}
		if (arr1.length != arr2.length) {
			return false;
		}
		for (int i = 0; i < arr1.length; i++) {
			if (arr1[i] != arr2[i]) {
				return false;
			}
		}
		return true;
	}

	public static void main(String[] args) {
		int[] arr = { 6, 9, 1, 3, 1, 2, 2, 5, 6, 1, 3, 5, 9, 7, 2, 5, 6, 1, 9 };
		int[] bfArr = getMinKNumsByBFPRT(arr, 10);
		Arrays.sort(bfArr);
		printArray(bfArr);
		int[] arr2 = { 6, 9, 1, 3, 1, 2, 2, 5, 6, 1, 3, 5, 9, 7, 2, 5, 6, 1, 9 };
		int[] ttArr = comparator(arr2, 10);
		Arrays.sort(ttArr);
		printArray(ttArr);

		int testTime = 500000;
		int maxSize = 100;
		int maxValue = 100;
		boolean succeed = true;
		for (int i = 0; i < testTime; i++) {
			int k = (int) (Math.random() * 50) - 5;
			int[] arr3 = generateRandomArray(maxSize, maxValue);
			int[] arr4 = copyArray(arr3);
			int[] bfrArr = getMinKNumsByBFPRT(arr3, k);
			int[] brrArr = comparator(arr4, k);
			Arrays.sort(bfrArr);
			Arrays.sort(brrArr);
			if (!isEqual(bfrArr, brrArr)) {
				succeed = false;
				printArray(bfrArr);
				printArray(brrArr);
				break;
			}
		}
		System.out.println(succeed ? "Nice!" : "Fucking fucked!");

		int[] arrTest = generateRandomArray(maxSize, maxValue);
		Arrays.sort(arrTest);
		printArray(arrTest);
		int k = (int) (Math.random() * 50) - 5;
		int[] testArr = getMinKNumsByBFPRT(arrTest, k);
		printArray(testArr);
	}

}
